-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: select random free server ports with 0 #960
test: select random free server ports with 0 #960
Conversation
8c8bc67
to
82726da
Compare
The previous port selection algorithm relied on port range. The range was from 48700 to 49000. This is 300 ports of which some may be already reserved in the system running tests. The free ports are allocated between the pytest workers. With 4 workers there is 75 ports for each. This causes flakiness in the tests as ports may not be available or are still in wait after previous tests. In the test environment it is not necessary to select ports from IANA dynamic port assignment range. Any free port is ok and port 0 can be used for dynamically allocated free port.
82726da
to
3e2202e
Compare
@@ -15,8 +14,15 @@ | |||
import socket | |||
|
|||
|
|||
@contextmanager | |||
def allocate_port_no_reuse() -> Iterator[int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit-pick]
Maybe Generator[int]
as the return type, since you already did earlier, but doesn't hurt as generators as also iterators.
def allocate_port_no_reuse() -> Iterator[int]: | ||
"""Allocate random free port and do not allow reuse.""" | ||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | ||
sock.bind(("127.0.0.1", 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If host or port are ‘’ or 0 respectively the OS default behavior will be used.
Simpler and nice 👍
"""Allocate random free port.""" | ||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | ||
sock.bind(("127.0.0.1", 0)) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
SO_REUSEADDR
flag tells the kernel to reuse a local socket inTIME_WAIT
state, without waiting for its natural timeout to expire.
About this change - What it does
The previous port selection algorithm relied on port range. The range
was from 48700 to 49000. This is 300 ports of which some may be already
reserved in the system running tests. The free ports are allocated between
the pytest workers. With 4 workers there is 75 ports for each. This causes
flakiness in the tests as ports may not be available or are still in
wait after previous tests. In the test environment it is not necessary
to select ports from IANA dynamic port assignment range. Any free port
is ok and port 0 can be used for dynamically allocated free port.